package org.parademo.equations.implementations;

import org.parademo.equations.base.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Random;

public class TestEquation_Generate implements TestEquationInterface {
	
	public static final double e = 2.71828182845904523536;
	
	/**^
	 * To set the number of voters.
	 */
	public int TOTAL_VOTERS = 100;
	
	/**
	 * Weighted votes for each voter, calculated and updated.
	 */
	ArrayList<Double> weightedAverages = new ArrayList<Double>(TOTAL_VOTERS);
	
	/**
	 * Individual randomly generated voting timings and tokens. 
	 */
	int[] timings = new int[TOTAL_VOTERS];
	int[] tokens = new int[TOTAL_VOTERS];
	
	/**
	 * Used for generating the tokens and list of timings.
	 */
	Random randomGenerator = new Random();

	/**
	 * Weighted totals for and against the proposal.
	 */
	double Wa = 0.0;
	double Wf = 0.0;
	
	/**
	 * Number of votes.
	 */
	int plusVotes = 0;
	int negativeVotes = 0;
	
	/**
	 * Total of tokens.
	 */
	int plusTotal = 0;
	int negativeTotal = 0;
	
	/**
	 * Temporary variables.
	 */
	int x = 0,y = 0;

	/**
	 * Implementation of the function.
	 */
	public double calculate(double index) {
		
		double result =  (-1 * Math.pow(index, 2.0) )/(1900 - Math.pow(index, 2.0));
		return Math.pow(e, result);
	}

	public void updateWeightedAverage() {
		
		double temp = 0.0;
		
		for (int i=0; i < TOTAL_VOTERS; i++) {
			
			x = (randomGenerator.nextBoolean() ? 1:-1) * randomGenerator.nextInt(6);
			//System.out.println("The value of token is : " + x + "\n");
			y = randomGenerator.nextInt(30);
			//System.out.println("The value of timing is : " + y + "\n");
			
			this.tokens[i] = x;
			this.timings[i] = y;
			
			if (x > 0) {
				
				plusVotes++;
				plusTotal += x;
				
				temp = x * calculate(y);
				this.weightedAverages.add(new Double(temp));
				
				Wf += temp;
				
			} else if (x < 0) {
				
				negativeVotes++;
				negativeTotal += x;
				
				temp = x * calculate(y);
				this.weightedAverages.add(new Double(temp));
				
				Wa += temp;
				
			} else if (x == 0)
				this.weightedAverages.add(0.0);
			
		}

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		TestEquation_Generate test = new TestEquation_Generate();
		
		test.updateWeightedAverage();
		
		Writer output = null;
	    File file = new File("output.txt");
	    
	    try {
			output = new BufferedWriter(new FileWriter(file));
			String t1 = "";
			String t2 = "";
			for (int ind : test.timings) {
				
				t1 += (test.timings[ind] + ",");
				t2 += (test.tokens[ind] + ",");
			}

            output.write ("Output of TestEquation_Generate \n");
            output.write ("-------------------------------\n\n");
			output.write ("The total votes for : "+ test.plusVotes + "\n");
			output.write ("The total votes against : "+ test.negativeVotes + "\n");
			
			output.write ("The total tokens for : "+ test.plusTotal + "\n");
			output.write ("The total tokens against : "+ test.negativeTotal + "\n");
			
			output.write ("The weighted total for : "+ test.Wf + "\n");
			output.write ("The weighted total against : "+ test.Wa + "\n");
			
			output.write("The timings array : "+ t1 + "\n");
			output.write("The tokens array : "+ t2);
			
			output.close();
			
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		
		System.out.println ("The total votes for : "+ test.plusVotes);
		System.out.println ("The total votes against : "+ test.negativeVotes);
		
		System.out.println ("The total tokens for : "+ test.plusTotal);
		System.out.println ("The total tokens against : "+ test.negativeTotal);
		
		System.out.println ("The weighted total for : "+ test.Wf);
		System.out.println ("The weighted total against : "+ test.Wa);

	}

}
